package com.ontheroad.core.util;

import com.ontheroad.path.model.Point;
import org.alternativevision.gpx.GPXParser;
import org.alternativevision.gpx.beans.GPX;
import org.alternativevision.gpx.beans.Track;
import org.alternativevision.gpx.beans.Waypoint;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * Created by kedong on 2017/8/14 0014.
 */
public class GpxUtils {

    public static String getGpx(List<Point> points) throws Exception {
        if (points != null && points.size() > 0) {
            Track track = new Track();
            ArrayList<Waypoint> waypoints = new ArrayList<>();
            for (Point point : points) {
                Waypoint waypoint = new Waypoint();
                waypoint.setLongitude(point.getLng());
                waypoint.setLatitude(point.getLat());
                waypoint.setElevation(point.getElevation());
                waypoint.setName(point.getName());
                waypoint.setComment(point.getComment());
                waypoint.setTime(point.getTime());
                waypoints.add(waypoint);
            }
            track.setTrackPoints(waypoints);
            GPX gpx = new GPX();
            HashSet<Track> tracks = new HashSet<Track>();
            tracks.add(track);
            gpx.setTracks(tracks);
            GPXParser gpxParser = new GPXParser();
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            gpxParser.writeGPX(gpx, bo);
            String gpxStr = new String(bo.toByteArray(), "UTF-8");
            return gpxStr;
        }
        return "";
    }
}
